# dwm - dynamic window manager
# See LICENSE file for copyright and license details.
MAKE_DIR:=$(shell pwd)   # := 的意思是将$(shell pwd) 表示为make_dir，类似于宏定义
HOME_DIR:=$(shell echo ${HOME})   # 此种方法下 sudo 下，Fefora Archlinux 获得的是/root
# USER_NAME:=$(shell echo ${USER})   # 此种方法下 sudo 下，Fefora Archlinux 获得的是 root
# SUDO_USER_NAME:=$(shell echo ${SUDO_USER})   # 此种方法下 sudo 下，Fefora Archlinux 获得的是 不是root而是执行sudo的用户
# SUDO_USER_DIR:=$(shell echo /home/${SUDO_USER})   
NO_ROOT_USER_DIR:=$(shell echo /home/${SUDO_USER})  
EXECUTOR := $(shell whoami)

# 假设有一个变量VERSION
VERSION := 1
# 条件赋值:当执行者是使用sudo执行时
ifeq ($(EXECUTOR), root) 
USER_HOME_DIR := $(NO_ROOT_USER_DIR)
else
USER_HOME_DIR := $(HOME_DIR)
endif
# 注意
# Makefile中，当 ifeq, else 和 endif 没有缩进时，make会正确识别它们，将其作为分支选择的标识
# 当 ifeq, else 和 endif 有缩进时，make将它们当做普通的shell script
# [Makefile 中的 ifeq 的缩进导致的错误_makefile ifeq syntax error near unexpected token-CSDN博客](https://blog.csdn.net/u012385733/article/details/73991933)

include config.mk

SRC = drw.c dwm.c util.c
OBJ = ${SRC:.c=.o}

all: options dwm

options:
	@echo dwm build options:
	@echo "CFLAGS   = ${CFLAGS}"
	@echo "LDFLAGS  = ${LDFLAGS}"
	@echo "CC       = ${CC}"

.c.o:
	${CC} -c ${CFLAGS} $<

${OBJ}: config.h config.mk

config.h:
	cp config.def.h $@

dwm: ${OBJ}
	${CC} -o $@ ${OBJ} ${LDFLAGS}

test:
# 	@echo MAKE_DIR is $(MAKE_DIR)
# 	@echo HOME_DIR is ${HOME_DIR}
# 	@echo USER_NAME is ${USER_NAME}
# 	@echo SUDO_USER_NAME is ${SUDO_USER_NAME}
# 	@echo SUDO_USER_DIR is ${SUDO_USER_DIR}
# 	@echo NO_ROOT_USER_DIR is ${NO_ROOT_USER_DIR}
	@echo EXECUTOR is ${EXECUTOR}
	@echo USER_HOME_DIR is ${USER_HOME_DIR}
# 	@if [ "$(EXECUTOR)" = "root" ]; then \
# 		echo "EXECUTOR  is  root";  \
# 	else \
# 		echo "EXECUTOR  is not root"; \
# 	fi

clean:
	rm -f dwm ${OBJ} dwm-${VERSION}.tar.gz  config.h 

dist: clean
	mkdir -p dwm-${VERSION}
	cp -R LICENSE Makefile README config.def.h config.mk\
		dwm.1 drw.h util.h ${SRC} dwm.png transient.c dwm-${VERSION}
	tar -cf dwm-${VERSION}.tar dwm-${VERSION}
	gzip dwm-${VERSION}.tar
	rm -rf dwm-${VERSION} config.h

install: all
	mkdir -p ${DESTDIR}${PREFIX}/bin
	cp -f dwm ${DESTDIR}${PREFIX}/bin
	chmod 755 ${DESTDIR}${PREFIX}/bin/dwm
	mkdir -p ${DESTDIR}${MANPREFIX}/man1
	sed "s/VERSION/${VERSION}/g" < dwm.1 > ${DESTDIR}${MANPREFIX}/man1/dwm.1
	chmod 644 ${DESTDIR}${MANPREFIX}/man1/dwm.1
	cp ./DWM.desktop  /usr/share/xsessions
	chmod 755 /usr/share/xsessions/dwm.desktop
	rm -rf $(join ${USER_HOME_DIR},/.dwm) 
	cp -r  ${MAKE_DIR} $(join ${USER_HOME_DIR},/.dwm) 

dev: all
# 	mkdir -p ${DESTDIR}${PREFIX}/bin
# 	cp -f dwm ${DESTDIR}${PREFIX}/bin
# 	chmod 755 ${DESTDIR}${PREFIX}/bin/dwm
# 	mkdir -p ${DESTDIR}${MANPREFIX}/man1
# 	sed "s/VERSION/${VERSION}/g" < dwm.1 > ${DESTDIR}${MANPREFIX}/man1/dwm.1
# 	chmod 644 ${DESTDIR}${MANPREFIX}/man1/dwm.1
# 	cp ./DWM.desktop  /usr/share/xsessions
# 	chmod 755 /usr/share/xsessions/DWM.desktop
# 	/bin/sh ./ln.sh 
# 	mkdir -p $(join ${USER_HOME_DIR},/.dwm) 
	rm -rf $(join ${USER_HOME_DIR},/.dwm) 
	mv $(join ${USER_HOME_DIR},/.dwm)   $(join ${USER_HOME_DIR},/.dwm-bak-$(shell echo `date +%y%m%d%H%M%S`) )
	ln -s $(join ${make_dir},)  $(join ${USER_HOME_DIR},/.dwm)


uninstall:
	rm -f ${DESTDIR}${PREFIX}/bin/dwm\
		${DESTDIR}${MANPREFIX}/man1/dwm.1\
		/usr/share/xsessions/dwm.desktop \

.PHONY: all options clean dist install uninstall

# [Makefile 中获取自身目录（pwd, lastword,dirname）_while_false_的博客-CSDN博客_makefile pwd](https://blog.csdn.net/qq_34595352/article/details/102954664)
# [Linux 下 sudo $HOME 改变问题解决 - akiyaの博客](https://little-star.love/posts/d3a9d9ff/)
# [如何获取Makefile中的用户名？ 中国服务器网](https://zgserver.com/makefile-26.html)

